home *** CD-ROM | disk | FTP | other *** search
/ BBS in a Box 3 / BBS in a box - Trilogy III.iso / Files / Prog / U-Z / VideoToolBox Folder / VideoToolboxSources / GDOpenWindow.c < prev    next >
Encoding:
Text File  |  1993-03-07  |  7.3 KB  |  177 lines  |  [TEXT/KAHL]

  1. /* GDOpenWindow.c
  2. Copyright © 1989-1993 Denis G. Pelli
  3.  
  4. SUMMARY:
  5.  
  6. AddExplicitPalette() adds a palette to a color window with all the colors marked
  7. as pmExplicit. This allows you to specify a color by its clut index, using
  8. PmForeColor() and PmBackColor(), for drawing operations, e.g. EraseRect() and
  9. DrawString().
  10.  
  11. GDOpenWindow1 opens a full-screen window on the specified screen, and calls
  12. AddExplicitPalette. The window truly fills the screen: any rounded corners are
  13. made square and if the window is on the main screen then the Menu Bar is
  14. hidden.
  15.  
  16. GDDisposeWindow1() closes and discards a color window, disposing of any palette
  17. or color table, generally undoing whatever GDOpenWindow1 did.
  18.  
  19. These routines return and accept a WindowPtr (even though it's actually
  20. a color window), which seems to be the standard way of doing things. 
  21. The older routines, without the "1" suffix, return and accept a CWindowPtr,
  22. which ends up forcing you to do a lot of tedious casting, e.g. when you call
  23. SetPort(), which wants a WindowPtr.
  24.  
  25. COMMENTS:
  26.  
  27. QuickDraw likes to pick a bunch of good colors and stuff them in the clut, in
  28. essentially random order, except that white is first and black is last. It wants
  29. you to specify any desired color as an RGB triplet and then it picks the mysterious
  30. clut index that would provide the closest match. If you're processing grayscale
  31. images, then QuickDraw's approach involves a lot of overhead involving inverse
  32. color tables, and makes the numbers stored in your pixels meaningless (unless
  33. you look them up in the associated color table or palette).
  34.  
  35. The philosophy of the VideoToolbox is to bypass QuickDraw's color model, and
  36. work explicitly with the numbers that are stored in your pixels.
  37. SetPixelsQuickly will efficiently poke (or peek) numbers in your PixMap.
  38. However, if you want to use QuickDraw's drawing operations, especially
  39. EraseRect() and DrawString() then you need a way to specify the foreground and
  40. background colors. AddExplicitPalette() gives your window a palette in which all
  41. the colors are marked as pmExplicit. This tells the palette manager not to
  42. meddle, and to use your arguments to PmForeColor() and PmBackColor() literally.
  43.  
  44. I suggest that you control the clut by calling GDUncorrectedGamma() and
  45. GDSetEntries(), since these calls directly control the video device driver,
  46. bypassing the Color Manager. Since the Color and Palette Managers don't know
  47. that you've changed the color environment they can't react to it, and will
  48. passively let you continue to specify colors by their clut index. For example,
  49. the Palette Manager religiously believes that the first clut entry should be
  50. white and the last one black, and it will change them back to those values if
  51. you change them and it finds out about it. If you use the Color Manager
  52. SetEntries call then the Paletter Manger WILL find out because a record is made
  53. in the ColorTable. Calling GDSetEntries() bypasses the Color Manager, instead
  54. the video device driver writes directly to the clut and the ColorTable is not
  55. modified. Of course, this means that you should ignore the ColorTable since it
  56. will no longer reflect the contents of the clut.
  57.  
  58. The Inside Mac books suggest that a new color window should be given its own
  59. color table. This will happen if you set MAKE_COLOR_TABLE true. However, for my
  60. purposes I think of the window and the screen as essentially the same thing, so
  61. I give the window a handle to the screen's color table, so they share the same
  62. table.
  63.  
  64. Every time you access the stdio package, e.g. printf or getch(), THINK C will
  65. move the Console window to the front, which may obscure your window. You can
  66. bring your window back to the front by calling BringToFront().
  67.  
  68. EXAMPLE:
  69.  
  70. Open your window by saying:
  71.     window=GDOpenWindow1(device);
  72. When you're through with the window, get rid of it by calling:
  73.     GDDisposeWindow1(window);
  74. Besides closing the window and disposing of the allocated memory structures it
  75. restores the device's clut to whatever is in the color table associated with device.
  76.  
  77. HISTORY:
  78.  
  79. 12/88        dgp    wrote it
  80. 8/5/89            added call to GDUncorrectedGamma, so I couldn't forget.
  81. 8/15/89     dgp trivia
  82. 3/20/90        dgp    make compatible with MPW C.
  83. 3/29/90        dgp    changed declared returned type from WindowPtr to CWindowPtr, which
  84.                 is what it's really been all along. Same change to argument of
  85.                 GDDisposeWindow(). The new offscreen GWorld calls for the first time
  86.                 make it easier to honestly declare one's windows as color rather
  87.                 than pretending they're not.
  88. 8/24/91        dgp    Made compatible with THINK C 5.0.
  89. 2/1/92        dgp Made optional the device argument to GDDisposeWindow(). If it's
  90.                 NULL, then it will be determined automatically from window.
  91. 2/3/93        dhb    Extracted AddExplicitPalette from GDOpenWindow.
  92. 2/21/93        dgp    HideMenuBar if window is on main screen.
  93. 2/23/93        dgp AddExplicitPalette() returns immediately unless it receives a color 
  94.                 window.
  95.                 Added GDOpenWindow1() and GDDisposeWindow1(), which both use
  96.                 a WindowPtr, instead of a less convenient CWindowPtr.
  97. 3/5/93        dgp    Added calls to UnclipScreen() and RestoreScreenClipping(), so the window
  98.                 now truly fills the whole screen. Edited GDOpenWindow() for clarity.
  99. 3/7/83        dgp    Added calls to GDSaveGamma(device) and GDRestoreGamma(device).
  100. */
  101. #include "VideoToolbox.h"
  102.  
  103. #define MAKE_COLOR_TABLE 0
  104.  
  105. WindowPtr GDOpenWindow1(GDHandle device)
  106. {
  107.     return (WindowPtr)GDOpenWindow(device);
  108. }
  109.  
  110. void GDDisposeWindow1(WindowPtr window)
  111. {
  112.     GDDisposeWindow(NULL,(CWindowPtr) window);
  113. }
  114.  
  115.  
  116. CWindowPtr GDOpenWindow(GDHandle device)
  117. {
  118.     WindowPtr window;
  119.     Rect r;
  120.     GDHandle oldDevice;
  121.  
  122.     if(device==NULL)return NULL;
  123.     r=(*device)->gdRect;        // rect of desired screen in global coordinates
  124.     GDSaveGamma(device);
  125.     GDUncorrectedGamma(device);
  126.     UnclipScreen(device);
  127.     oldDevice=GetGDevice();
  128.     SetGDevice(GetMainDevice());
  129.     window=NewCWindow(NULL,&r,"\pHi",TRUE,plainDBox,(WindowPtr) -1L,0,123L);
  130.     SetGDevice(oldDevice);
  131.     AddExplicitPalette(window);
  132.     return (CWindowPtr)window;
  133. }
  134.  
  135. void GDDisposeWindow(GDHandle device,CWindowPtr window)
  136. // Dispose of window and palette and restore the clut.
  137. {
  138.     if(window==NULL)return;
  139.     if(device==NULL)device=GetWindowDevice((WindowPtr)window);
  140.     DisposePalette(GetPalette((WindowPtr)window));
  141.     #if MAKE_COLOR_TABLE
  142.         DisposHandle((Handle)(*((CGrafPtr)window)->portPixMap)->pmTable);
  143.     #endif
  144.     DisposeWindow((WindowPtr)window);
  145.     if(device==NULL)return;
  146.     GDRestoreGamma(device);
  147.     GDRestoreDeviceClut(device);
  148.     RestoreScreenClipping(device);
  149. }
  150.  
  151. void AddExplicitPalette(WindowPtr window)
  152. // Create a palette for the color window and mark all the entries as explicit.
  153. // Copy the entries from the window's device.
  154. {
  155.     GDHandle device;
  156.     CTabHandle cTabHandle;
  157.     PaletteHandle palette;
  158.     int colors,i;
  159.     OSErr error;
  160.  
  161.     if(window==NULL)return;
  162.     if(((CGrafPtr)window)->portVersion>=0) return;    // Not a color window, return.
  163.     device=GetWindowDevice(window);
  164.     if(device==NULL)PrintfExit("AddExplicitPalette: window has no device!\n");
  165.     cTabHandle=(**(**device).gdPMap).pmTable;
  166.     colors=(*cTabHandle)->ctSize+1;
  167.     #if MAKE_COLOR_TABLE
  168.         error=HandToHand((Handle*)&cTabHandle);
  169.         if(error)PrintfExit("AddExplicitPalette: error %d in copying color table\n",error);
  170.         for(i=0;i<colors;i++) (*cTabHandle)->ctTable[i].value=i;
  171.         (*cTabHandle)->ctFlags &= 0x7fff;
  172.         (*cTabHandle)->ctSeed=GetCTSeed();
  173.         (*((CGrafPtr)window)->portPixMap)->pmTable=cTabHandle;
  174.     #endif
  175.     palette=NewPalette(colors,cTabHandle,pmExplicit,0);
  176.     SetPalette(window,palette,0);
  177. }